package com.jianyun.jyzs.widget;

import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.jianyun.jyzs.R;
import com.jianyun.jyzs.bean.LeaveMenuBean;
import com.jianyun.jyzs.utils.DensityUtil;

import java.util.ArrayList;

/**
 * Created by yangshijun on 2018/9/19.
 */

public class RightTopDialog extends PopupWindow {

    private Activity mContext;
    private View conentView;
    public OnListViewLinsenter onListViewLinsenter;
    private final ListView listView;
    private final MyListAdapter myListAdapter;

    public RightTopDialog(Context context, final OnListViewLinsenter onListViewLinsenter, ArrayList<LeaveMenuBean> leaveMenuBeans) {
        super(context);
        this.mContext = (Activity) context;
        this.onListViewLinsenter = onListViewLinsenter;
        conentView = View.inflate(mContext, R.layout.view_right_diolog, null);
        listView = conentView.findViewById(R.id.diolog_listview);
        myListAdapter = new MyListAdapter(context, leaveMenuBeans);
        listView.setAdapter(myListAdapter);
        int h = mContext.getWindowManager().getDefaultDisplay().getHeight();
        int w = mContext.getWindowManager().getDefaultDisplay().getWidth();
        int width= DensityUtil.dip2px(context,100);

        // 设置SelectPicPopupWindow的View
        this.setContentView(conentView);
        // 设置SelectPicPopupWindow弹出窗体的宽
        this.setWidth(width);
        // 设置SelectPicPopupWindow弹出窗体的高
        this.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
        // 设置SelectPicPopupWindow弹出窗体可点击
        this.setFocusable(true);
        this.setOutsideTouchable(true);
        // 刷新状态
        this.update();
        // 实例化一个ColorDrawable颜色为半透明
        ColorDrawable dw = new ColorDrawable(0000000000);
        // 点back键和其他地方使其消失,设置了这个才能触发OnDismisslistener ，设置其他控件变化等操作
        this.setBackgroundDrawable(dw);
        // mPopupWindow.setAnimationStyle(android.R.style.Animation_Dialog);
        // 设置SelectPicPopupWindow弹出窗体动画效果
        this.setAnimationStyle(R.style.AnimationPreview);
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                onListViewLinsenter.onclickPosition(position);
                RightTopDialog.this.dismiss();
            }
        });

    }

    public void setLeaveBane(ArrayList<LeaveMenuBean> leaveMenuBeans){
        myListAdapter.setLeaveMenuBeans(leaveMenuBeans);
    }

    public void showPopupWindow(View parent) {
        if (!this.isShowing()) {
            int par=parent.getLayoutParams().width / 2;
            Log.i("test","偏移："+par);
            this.showAsDropDown(parent, -100, 0);
            WindowManager.LayoutParams lp = mContext.getWindow().getAttributes();
            lp.alpha = 0.7f;
            mContext.getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);//多加这一句，问题就解决了！这句的官方文档解释是：让窗口背景后面的任何东西变暗
            mContext.getWindow().setAttributes(lp);
            //此处作为点击PopupWindow之外的地方取消掉背景变暗
            this.setOnDismissListener(new OnDismissListener() {

                @Override
                public void onDismiss() {
                    WindowManager.LayoutParams lp = mContext.getWindow().getAttributes();
                    lp.alpha = 1f;
                    mContext.getWindow().setAttributes(lp);
                }
            });
        } else {
            this.dismiss();
        }
    }

    /**
     * 接口回调
     */
    public interface OnListViewLinsenter {
        void onclickPosition(int i);
    }

    class MyListAdapter extends BaseAdapter {

        private ArrayList<LeaveMenuBean> leaveMenuBeans;

        private void setLeaveMenuBeans(ArrayList<LeaveMenuBean> leaveMenuBeans){
            this.leaveMenuBeans=leaveMenuBeans;
            notifyDataSetChanged();
        }

        public MyListAdapter(Context context, ArrayList<LeaveMenuBean> leaveMenuBeans) {
            leaveMenuBeans = leaveMenuBeans;
        }

        @Override
        public int getCount() {
            return leaveMenuBeans == null ? 0 : leaveMenuBeans.size();
        }

        @Override
        public Object getItem(int position) {
            return null;
        }

        @Override
        public long getItemId(int position) {
            return 0;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            MyViewHolder viewHolder;
            if (convertView == null) {
                viewHolder = new MyViewHolder();
                convertView = View.inflate(mContext, R.layout.item_right_top_dialog, null);
                viewHolder.topRight = convertView.findViewById(R.id.top_right_text);
                convertView.setTag(viewHolder);
            } else {
                viewHolder = (MyViewHolder) convertView.getTag();
            }
            viewHolder.topRight.setText(leaveMenuBeans.get(position).getText());

            return convertView;
        }
    }


    class MyViewHolder {
        TextView topRight;
    }
}
